<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\validate\contract;

use app\helper\Regular;
use think\Validate;

class ContractBasicValidate extends Validate
{
    protected $rule = [
        'property_id|物业信息' => 'require',
        'room_id|房间信息' => 'require',
        'number|租赁编号' => 'require',
        'start_date|租赁开始时间' => 'require',
        'end_date|租赁结束时间' => 'require|gt:start_date',
        'rent_date|计租生效日期' => 'require',
        'room_rent|租金' => 'require|float',
        'room_deposit|押金' => 'require|float',
        'bill_add_day|账单生成日' => 'require',
        'bill_add_day|租金创建日期' => 'require|number',
        'pay_type|租金支付方式' => 'require|in:1,2,3',
        'bill_month|账单生成方式' => 'require|in:1,2,3',
        'out_fee_flag|银行手续费承担方式' => 'require|in:1,2',
        'sign_type|合同类型' => 'require|in:1,2',
        'end_method|退租方式' => 'require|in:1,2',
        'lessee_info' => 'checkLesseeInfo'
    ];

    public function sceneAdd()
    {
        return $this->remove('id', 'require');
    }

    public function sceneEdit()
    {
        return $this->remove('id', 'require')
            ->remove('room_rent', 'require')
            ->remove('pay_type', 'require')
            ->remove('room_deposit', 'require')
            ->remove('start_date', 'require')
            ->remove('end_date', 'require')
            ->remove('rent_date', 'require')
            ->remove('number', 'require')
            ->remove('room_id', 'require')
            ->remove('property_id', 'require')
            ->remove('lessee_info', 'require');
    }

    public function sceneDel()
    {
        return $this->only(['id'])->append('id', 'require|number');
    }


    public function checkLesseeInfo($value)
    {
        if (!is_array($value)) {
            return '住户数据格式错误';
        }
        $fields = [
            'username', 'mobile', 'is_type', 'front_file', 'back_file'
        ];
        if ($value['username'] == '') {
            return '承租人姓名不能为空';
        }
        if (!Regular::mobile($value['mobile'])) {
            return '承租人手机号格式错误';
        }
        $certificateType = $value['is_type'] ?? 1;
        if (isset($value['number']) && $value['number']) {
            if ($certificateType == 1 && !Regular::idcardVerify($value['number'])) {
                return '承租人身份证格式错误';
            }
            if ($certificateType == 2 && !Regular::passportVerify($value['number'])) {
                return '承租人护照格式错误';
            }
            if ($certificateType == 3 && !Regular::gapassportVerify($value['number'])) {
                return '承租人港澳通信证格式错误';
            }
        }
        $dataFields = array_keys($value);
        foreach ($fields as $field) {
            if (!in_array($field, $dataFields)) {
                return '承租人数据不完整';
            }
        }
        return true;
    }

    /**
     *  合同审核
     * @return ContractBasicValidate
     *
     * @author Amos
     * 2024-06-13 22:05
     */
    public function sceneAudit()
    {
        return $this->only(['status', 'remark'])
            ->append('status|审核状态', 'require|in:7,2,5')
            ->append('remark|备注说明', 'requireIf:status,2');
    }

    /**
     * 合同退租
     * @return ContractBasicValidate
     *
     * @author Amos
     * 2024-06-13 22:05
     */
    public function sceneRenun()
    {
        return $this->only(['refund_type', 'remark'])
            ->append('refund_type|审核状态', 'require|in:1,2')
            ->append('remark|备注说明', 'requireIf:refund_type,2');
    }

    /**
     * 合同作废
     * @return ContractBasicValidate
     *
     * @author Amos
     * 2024-06-13 22:05
     */
    public function sceneInvalid()
    {
        return $this->only(['remark'])
            ->append('remark|备注说明', 'require');
    }

}